let requestUrl;
let width = 45;

function addHandler() {
	requestUrl = "/school/insert";

	$("#school_dialog").dialog("open");
}

function editHandler() {
	let data = $("#school_list").datagrid("getSelected");

	if (data) {
		$("#id").val(data.id);
		$("#name").textbox("setValue", data.name);
		$("#note").textbox("setValue", data.note);
		$("#phyle").combobox("setValue", data.phyle);
		$("#founderId").combobox("setValue", data.founderId);

		requestUrl = "/school/updateById";

		$("#school_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录。", "warning");
	}
}

function imageHandler() {
	let rowData = $("#school_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/school/updateById";

		$("#school_id").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录。", "warning");
	}
}

$(document).ready(function() {

	$("#_phyle_").combobox({
		url: "/phyle/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "-请选择种族-",
	});

	// 搜索按钮
	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let phyle = $("#_phyle_").combobox("getValue");

		$("#school_list").datagrid("load", {
			phyle: phyle
		});
	});

	// 清空按钮
	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	$("#name").textbox({
		width: 120,
		required: true
	});

	$("#phyle").combobox({
		url: "/phyle/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto"
	});

	$("#founderId").combobox({
		url: "/school_founder/selectAll",
        valueField: "id",
        textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto"
	});
	
	$("#note").textbox({
		width: 320,
    	height: 100,
    	required: true,
		multiline: true
	});
	
	$("#school_dialog").dialog({
		title: "门派信息",
		modal: true,
		closed: true,
		closable: false,
		draggable: false,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#school_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(response) {
						showMsg(response.message);

						$(selector).form("clear");
						$("#school_dialog").dialog("close");
						$("#school_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#school_dialog").dialog("close");
				$("#school_form").form("clear");
			}
		}]
	});
	
	$("#upload").filebox({
		buttonText: "选择文件",
		width: 240,
		required: true,
		onChange: function() {
			let rowData = $("#school_list").datagrid("getSelected");

			if(rowData) {
				fileUpload(this, rowData.id, "/school/upload");
			}
		}
	});

	fetchUploadDialog(requestUrl, function () {
		$("#school_list").datagrid("reload");
	});

	$("#school_list").datagrid({
		url: "/school/selectByPage",
		title: "门派列表",
		height: table_height_with_search,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", buttonUpload],
		columns: [[
			{field: "id", title: "编号", align: "center"},
			{field: "name", title: "门派名称", align: "center", width: 30},
			{field: "image", title: "图片", align: "center", width: 30
				, formatter: function(value) {
					return "<img alt='' height='" + size + "' src='" + value + "' />";
				}
			},
			{field: "founderId", title: "门派师父", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/school_founder/selectNameById", {
						id: value
					},function(response) {
						$("#founder_" + rowIndex).html(response.data);
					}, error);
					
					return "<div id='founder_" + rowIndex + "'></div>";
				}
			},
			{field: "phyle", title: "种族", align:"center", width: 30
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/phyle/selectById", {
						id: value
					}, function(resp) {
						$("#phyle_" + rowIndex).html(resp.data.name);
					}, error);

					return "<div id='phyle_" + rowIndex + "'></div>";
				}
			},
			{field: "note", title: "门派简介", align: "center", width: 200
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			}
		]]
	});

});